package com.android.DSViews;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class DSViewsActivity extends Activity implements OnClickListener {
	
	private SearchView mSearchView;
	private Button mButton;
	private GLSurfaceView mGLSurfaceView;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewer3d);
        mButton = (Button)findViewById(R.id.SettingsBtn);
        mButton.setOnClickListener(this);
/*        mGLSurfaceView = new GLSurfaceView(this);
//        if (detectOpenGLES20()) {
            // Tell the surface view we want to create an OpenGL ES 2.0-compatible
            // context, and set an OpenGL ES 2.0-compatible renderer.
            mGLSurfaceView.setEGLContextClientVersion(2);
            mGLSurfaceView.setRenderer(new GLES20TriangleRenderer(this));
//        } else {
            // Set an OpenGL ES 1.x-compatible renderer. In a real application
            // this renderer might approximate the same output as the 2.0 renderer.
           // mGLSurfaceView.setRenderer(new TriangleRenderer(this));
//        }
        setContentView(mGLSurfaceView);
*/        
    }
    
    private boolean detectOpenGLES20() {
        ActivityManager am =
            (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        ConfigurationInfo info = am.getDeviceConfigurationInfo();
        return (info.reqGlEsVersion >= 0x20000);
    }
    
    @Override
    protected void onResume() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onResume();
        mGLSurfaceView.onResume();
    }

    @Override
    protected void onPause() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onPause();
        mGLSurfaceView.onPause();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)	{
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu,menu);
  //  	mSearchView = (SearchView) menu.findItem(R.menu.menu).getActionView();
    	return true;
    }
    
    public InputStream getInputStream() {
    	
        InputStream is;
		try {
			is = getAssets().open("teddy.obj");
			return is;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
    }
    
    public void onClick(View v)
    {
      ObjReader objreader = new ObjReader();
      try {
			List<Vertex> vertices = objreader.readMesh(getInputStream());
			TextView tv = (TextView) findViewById(R.id.VertexList);
			tv.setText(vertices.get(1).toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}